package com.haorui.lygyd.controller;

import com.haorui.base.core.exception.BusinessException;
import com.haorui.base.system.entity.Account;
import com.haorui.base.system.service.AccountService;
import com.haorui.fileserver.FileSaveUtils;
import com.haorui.lygyd.employeemanager.entity.MemberEntity;
import com.haorui.lygyd.employeemanager.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 陈刚
 * 日期: 2017/10/10
 * 时间: 23:07
 * 功能：请进行修改
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private FileSaveUtils fileSaveUtils;

    @RequestMapping(value = "account/uploadPhoto/{aid}", method = RequestMethod.POST)
    public String uploadAccountPhoto(@PathVariable("aid") String aid,@RequestParam("photo") MultipartFile photo,HttpServletRequest request) throws BusinessException {
        Account account=accountService.findOne(aid);
        if(account==null){
            throw new BusinessException("账号不存在！");
        }
        if(photo==null){
            throw new BusinessException("头像不能为空！");
        }
        try {
            String photoPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
            photoPath+=fileSaveUtils.saveAccountPhoto(photo,account);
           account.setPhoto(photoPath);
           accountService.save(account);
           return account.getPhoto();
        } catch (IOException e) {
            throw new BusinessException("上传头像失败!");
        }

    }
}
